//
//  WelcomeViewController.swift
//  新浪微播
//
//  Created by lxw on 15/12/18.
//  Copyright © 2015年 lxw. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        //将根视图 ＝ 背景视图
        view = backImageView
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()
    }
    
    
    
    private func showAnimation(){
        //设置label 透明度 为 0
        welcomeLabel.alpha = 0
        let offset = -UIScreen.mainScreen().bounds.height+200
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.iconView.snp_updateConstraints{ (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset (offset)
            }
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("OK")
                        //动画结束的时候 发出页面切换的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //设置iconView 对应的图片
        iconView.sd_setImageWithURL(UserAccountViewModel().headURL,placeholderImage:UIImage(named: "avatar_default_big"))
         //设置图片的圆角
        iconView.layer.cornerRadius = 45
        //裁减
        iconView.layer.masksToBounds = true
    }
    
    
//  MARK:懒加载所有的子视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var welcomeLabel: UILabel = {
        let l = UILabel()
        l.text = (UserAccountViewModel() . userName ?? "") + "欢迎归来"
        l.font = UIFont.systemFontOfSize(16)
        l.textColor = UIColor .lightGrayColor()
        //设置大小
        l.sizeToFit()
        return l 
    }()
    }
    









